//定义构造函数生成己方飞机
function Fly(image) {
	this.x = myCanvas.width / 2 - 33;
	this.y = myCanvas.height - 82;
	this.w = 66;
	this.h = 82;
	this.blood = 5;
	this.positioX = 0; //存储截取区域的起始的X坐标
	this.image = image;
	this.bullets = []; //存储当前飞机对象绘制的所有子弹
	this.count = 0; //记录当前绘制的频率
	this.totleCount = 5; //总频率
	
	this.kind = 1; //存储子弹的类别
	this.time = 0; //双排子弹的绘制数量
	
	//定义属性clear用来记录当前飞机对象是否正在执行界面净化操作，默认false
	this.clear = false;
	
	this.collisionEenmy = null; //存储相撞的敌机对象
}
//定义函数绘制飞机的样式
Fly.prototype.draw = function() {
		ctx.drawImage(this.image, this.positioX, 0, this.w, this.h, this.x, this.y, this.w, this.h);
	}
	//定义函数为己方飞机添加一个功能用来绘制自身的子弹的
Fly.prototype.drawBullet = function(image) {
		this.count++;
		if(this.count % this.totleCount == 0) {
			this.count = 0;
			var bullet = null;
			if(this.kind == 1) {
				bullet = new Bullet(image, this.x + this.w / 2 - 3, this.y - 14, -5);
			} else if(this.kind == 2) {
				bullet = new Bullet(image,this.x+this.w/2-24,this.y-14,-5);
				this.bullets.push(bullet);
				bullet = new Bullet(image,this.x+this.w/2+18,this.y-14,-5);
				//双排子弹绘制结束，此时开始绘制单排子弹
				this.time--;
				if(this.time <= 0) {
					this.kind = 1;
				}
			}
			this.bullets.push(bullet);
		}
	}
	//定义一个方法完成飞机子弹的移动
Fly.prototype.moveBullets = function() {
		for(var i = 0; i < this.bullets.length; i++) {
			this.bullets[i].move(); //移动子弹对象
			if(this.bullets[i].y <= -14) {
				this.bullets.splice(i, 1);
				i--;
			} else {
				//绘制子弹对象
				this.bullets[i].draw();
			}
		}
	}
	//定义一个功能完成子弹对象和敌机对象的碰撞检测
Fly.prototype.adjustBECollision = function(enemys) {
	for(var i = 0; i < this.bullets.length; i++) {
		for(var j = 0; j < enemys.length; j++) {
			if(i >= 0) {
				if(this.bullets[i].x >= (enemys[j].x - this.bullets[i].w) && this.bullets[i].x <= (enemys[j].x + enemys[j].w) && this.bullets[i].y <= (enemys[j].y + enemys[j].h) && this.bullets[i].y >= (enemys[j].y-14)) {
					//代表子弹和敌机相撞
					this.bullets.splice(i, 1);
					i--;
					enemys[j].blood--;
					enemys[j].positionX += enemys[j].w;
					if(enemys[j].blood == 0) {
						enemys.splice(j,1);
						j--;
					}
				}
			}else {
				break;
			}
		}
	}
}
//定义方法完成飞机的清屏操作
Fly.prototype.clearScreen = function(enemys) {
    for(var i = 0; i < enemys.length; i++) {
    	    enemys[i].blood--;
    	    enemys[i].positionX += enemys[i].w;
    	    if(enemys[i].blood == 0) {
    	    	   enemys.splice(i,1);
    	    	   i--;
    	    }
    	    if(enemys.length == 0) {
    	    	    //屏幕净化结束
    	    	    this.clear = false;
    	    }
    }
}
//为己方飞机添加和礼品的碰撞检测过程
Fly.prototype.adjustFGCollision = function(gifts) {
	var centerX1 = this.x+this.w/2;
	var centerY1 = this.y+this.h/2;
	for(var i = 0; i < gifts.length; i++) {
		var centerX2 = gifts[i].x+gifts[i].w/2;
		var centerY2 = gifts[i].y+gifts[i].h/2;
		var disX = Math.abs(centerX1-centerX2);
		var disY = Math.abs(centerY1-centerY2);
		if(disX <= (this.w+gifts[i].w)/2 && disY<=(this.h+gifts[i].h)/2) {
			//判断碰撞的是否是双排子弹
			if(gifts[i].positionX == 39) {
				this.kind = 2;
				this.time += 100;
			}else if(gifts[i].positionX == 0) {
				this.clear = true;
			}
			gifts.splice(i,1);
			i--;
		}
	}
}
//添加功能完成自己的飞机和敌机的碰撞检测
Fly.prototype.adjustFECollision = function(enemys) {
	var centerX1 = this.x+this.w/2;
	var centerY1 = this.y+this.h/2;
	var collision = false; //存储是否相撞
	for(var i = 0; i < enemys.length; i++) {
		var centerX2 = enemys[i].x+enemys[i].w/2;
		var centerY2 = enemys[i].y+enemys[i].h/2;
		var disX = Math.abs(centerX1-centerX2);
		var disY = Math.abs(centerY1-centerY2);
		if(disX <= (this.w+enemys[i].w)/2 && disY <= (this.h+enemys[i].h)/2) {
			this.collisionEenmy = enemys[i];
			collision = true;
			break;
		}
	}
	return collision;
}